package com.liu.resume.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;

import com.liu.resume.MyEvent;
import com.liu.resume.R;
import com.liu.resume.ui.base.BaseActivity;
import com.liu.resume.ui.base.BlurActivity;
import com.liu.resume.ui.contract.LoginContract;
import com.liu.resume.ui.presenter.LoginPresenterImpl;
import com.liu.resume.util.ImageUtil;
import com.liu.resume.util.RegexUtils;
import com.liu.resume.util.ToastUtil;
import com.liu.resume.viewmodel.QueryAvatarViewModel;
import com.liu.resume.viewmodel.UserOperateViewModel;
import com.rengwuxian.materialedittext.MaterialEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/9/24.
 */
public class LoginActivity extends BlurActivity implements QueryAvatarViewModel.Listener, LoginContract.LoginView {
    @Bind(R.id.img_avatar)
    ImageView imgAvatar;
    @Bind(R.id.edit_user_name)
    MaterialEditText editUserName;
    @Bind(R.id.edit_password)
    MaterialEditText editPassword;

    QueryAvatarViewModel queryAvatarViewModel;
    private LoginContract.LoginPresenter mPresenter;


    private LoginPresenterImpl mLoginPresenter;


    public static void actionStart(BaseActivity context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.setBlur(context);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mLoginPresenter == null) {
            mLoginPresenter = new LoginPresenterImpl(this, this);
        }

        queryAvatarViewModel = new QueryAvatarViewModel(this);

    }

    @Override
    protected int getContentView() {

        return R.layout.activity_login;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        View view = getWindow().peekDecorView();
        //隐藏键盘
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
        editUserName.addTextChangedListener(new MyTextWatcher());
    }

    @Override
    protected void onInitToolBar() {
        super.onInitToolBar();
        mToolbar.setTitle("");
        mToolbar.setNavigationIcon(R.mipmap.ic_close_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mToolbar.inflateMenu(R.menu.menu_add);
    }

    @OnClick({R.id.img_user_agreement, R.id.tv_user_agreement, R.id.tv_forget_password, R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_user_agreement:
                break;
            case R.id.tv_user_agreement:
                break;
            case R.id.tv_forget_password:
                break;
            case R.id.btn_login:
                String username = editUserName.getText().toString().trim();
                String pwd = editPassword.getText().toString().trim();
                if (!RegexUtils.isPhone(username)) {
                    editUserName.setError(getString(R.string.error_regex_phone));
                } else if (!RegexUtils.isPassword(pwd)) {
                    editPassword.setError(getString(R.string.error_regex_password));
                } else {
//                    showLoadingDialog(getString(R.string.dialog_loading_login));
                    mLoginPresenter.start(username, pwd);
                }

                break;
        }
    }

    @Override
    public void onAvatarSuccess(String imgUrl) {
        ImageUtil.loadCircleImage(this, imgUrl, imgAvatar, R.mipmap.ic_avatar_128);
    }

    @Override
    public void onAvatarFail() {
        ImageUtil.loadResImage(this, R.mipmap.ic_avatar_128, imgAvatar);
    }


    @Override
    public void showProgress(String mess) {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void onLoginSuccess() {
        ToastUtil.show(getString(R.string.toast_success_login));
        EventBus.getDefault().post(MyEvent.UPDATE_USER);
        finish();
    }


    @Override
    public void setPresenter(LoginContract.LoginPresenter presenter) {
        this.mPresenter = presenter;
    }

    class MyTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (RegexUtils.isPhone(s.toString())) {
                //查询头像
                queryAvatarViewModel.getAvatar(s.toString());
            } else {
                ImageUtil.loadResCircleImage(LoginActivity.this, R.mipmap.ic_avatar_128, imgAvatar);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
